package com.itheima.mybatis01.dao;

import com.itheima.mybatis01.bean.Teacher;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MybatisDemo {
    @Test
    public void selectAll() throws Exception  {
        //加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        //获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        is.close();
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行配置文件中的sql 语句,并接收结果

        List<Teacher> list = sqlSession.selectList("TeacherMapper.selectAll");
        for (Teacher teacher : list) {
            System.out.println(teacher);
        }
        sqlSession.close();
    }
    @Test
    public void selectOne() throws Exception{
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Teacher teacher = sqlSession.selectOne("TeacherMapper.selectOne", 1);
        System.out.println(teacher);
    }

    @Test
    public void insert() throws IOException {
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        is.close();
        //需要手动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession1 = sqlSessionFactory.openSession(true);   自动提交事务
        Teacher teacher = new Teacher(2,"赵留法",26);
        int insert = sqlSession.insert("TeacherMapper.insert", teacher);
        System.out.println(insert);
        sqlSession.commit();
        sqlSession.close();
    }
    /*@Test
    public void insert1() throws IOException {
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        List<Teacher> list = new ArrayList<>();
        list.add(new Teacher(6,"王麻子",29));
        list.add(new Teacher(7,"王府滚",30));
        sqlSession.insert("TeacherMapper.insert1",list);
    }*/
    @Test
    public void update() throws IOException{
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Teacher teacher = new Teacher(4,"赵云飞",23);
        sqlSession.update("TeacherMapper.update",teacher);
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }
    @Test
    public void delete() throws IOException{
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        is.close();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int delete = sqlSession.delete("TeacherMapper.delete", 2);
        System.out.println(delete);
        sqlSession.commit();
        sqlSession.close();
    }
}
